package com.maiyuren.dict_page_view_plugin.dictpageview;

import android.content.Context;
import android.util.AttributeSet;
import com.maiyuren.dict_page_view_plugin.DictPageViewPlugin;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

import java.util.List;
import java.util.Map;

import android.util.Log;
import android.view.ViewGroup;

import android.widget.LinearLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class DictPageView extends LinearLayout implements ViewPager.OnPageChangeListener {
    DictPageViewAdapter _adapter = null;
    ViewPager _view = null;
    Integer _id = 0;

    public DictPageView(@NonNull Context context, Integer id) {
        super(context);
        _id = id;
        this.setLayoutParams(new LayoutParams(-1, -1));
        _view = new ViewPager(this.getContext());
        _view.setLayoutParams(new LayoutParams(-1, -1));
        _view.addOnPageChangeListener(this);
        this.addView(_view);
//        test();
    }

    public DictPageView(@NonNull Context context, AttributeSet as) {
        super(context, as);
        this.setLayoutParams(new LayoutParams(-1, -1));
        _view = new ViewPager(this.getContext());
        _view.setLayoutParams(new LayoutParams(-1, -1));
        _view.addOnPageChangeListener(this);
        this.addView(_view);
//        test();
    }

    private void test() {
        List<Map<String, Object>> test = new Vector<>();
        Map<String, Object> item1 = new HashMap<>();
        item1.put("name", "網·字源");
        item1.put("type", "web");
        item1.put("path", "https://www.cnblogs.com");
        test.add(item1);

        Map<String, Object> item2 = new HashMap<>();
        item2.put("name", "網·字源2");
        item2.put("type", "web");
        item2.put("path", "http://www.maiyuren.com/project/open-chinese-dict/?dict=ziyuan&word=1&notitle=1");
        test.add(item2);

        Map<String, Object> item3 = new HashMap<>();
        item3.put("name", "網·字源3");
        item3.put("type", "web");
        item3.put("path", "http://www.maiyuren.com/project/open-chinese-dict/?dict=ziyuan&word=2&notitle=1");
        test.add(item3);

        Map<String, Object> item4 = new HashMap<>();
        item4.put("name", "網·字源4");
        item4.put("type", "web");
        item4.put("path", "http://www.maiyuren.com/project/open-chinese-dict/?dict=ziyuan&word=2&notitle=1");
        test.add(item4);
        this.load(test);
    }

    public void load(List<Map<String, Object>> info) {
        _adapter = new DictPageViewAdapter(info, _id);
        _view.setAdapter(_adapter);
    }


    @Override
    public void onPageSelected(int pos) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", _id);
        param.put("tabIndex", pos % _adapter._info.size());


       DictPageViewPlugin.channel.invokeMethod("onPageSelected", utils.map2string(param), null);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void loadUrl(int tabIndex, String url) {
        if (_adapter != null) _adapter.loadUrl(tabIndex, url);
    }

    public void changeTab(int tabIndex) {
        if (_view != null) _view.setCurrentItem(tabIndex, false);
    }

    public byte[] takePhoto(int tabIndex) {
        if (_adapter != null) return _adapter.takePhoto(tabIndex);
        return null;
    }
}
